package com.socool.sinofaith.adapter;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.utils.DistanceUtil;
import com.socool.sinofaith.R;
import com.socool.sinofaith.model.SifTaskAddressBean;
import com.socool.sinofaith.ui.MapActivity;
import com.socool.sinofaith.ui.SubTaskActivity;
import com.socool.sinofaith.view.TiPsDialog;

import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class TaskAddressAdapter extends BaseAdapter {

	private List<SifTaskAddressBean> mdatas = new ArrayList<SifTaskAddressBean>();
	private LayoutInflater inflater;
	private boolean isShowStartTask = false;
	private LatLng localLatLng;

	public LatLng getLocalLatLng() {
		return localLatLng;
	}

	public void setLocalLatLng(LatLng localLatLng) {
		this.localLatLng = localLatLng;
	}

	public void setShowStartTask(boolean isShowStartTask) {
		this.isShowStartTask = isShowStartTask;
	}

	private Context mContext;

	public List<SifTaskAddressBean> getMdatas() {
		return mdatas;
	}

	public void setMdatas(List<SifTaskAddressBean> mdatas) {

		this.mdatas = mdatas;
	}

	public void addDatas(List<SifTaskAddressBean> mdatas) {
		this.mdatas.addAll(mdatas);
		this.notifyDataSetChanged();
	}

	public TaskAddressAdapter(Context _context, boolean isPublic) {
		super();
		mContext = _context;
		inflater = LayoutInflater.from(_context);
	}

	@Override
	public int getCount() {
		if (mdatas != null) {
			return mdatas.size();
		}
		return 0;
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup arg2) {

		ViewHolder mViewHolder = null;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.taskaddress_listitem, null);
			mViewHolder = new ViewHolder();
			mViewHolder.addressTextView = (TextView) convertView.findViewById(R.id.addressTextView);
			mViewHolder.distanceTextView = (TextView) convertView.findViewById(R.id.distanceTextView);
			mViewHolder.addressDetailsTextView = (TextView) convertView.findViewById(R.id.addressDetailsTextView);
			mViewHolder.watchAddressLinearLayout = (LinearLayout) convertView.findViewById(R.id.watchAddressLinearLayout);
			mViewHolder.taskButton = (Button) convertView.findViewById(R.id.taskButton);
			convertView.setTag(mViewHolder);

		} else {
			mViewHolder = (ViewHolder) convertView.getTag();
		}
		mViewHolder.addressTextView.setText(mdatas.get(position).getProvince() + mdatas.get(position).getCity());

		mViewHolder.addressDetailsTextView.setText(mdatas.get(position).getAddress());
		mViewHolder.watchAddressLinearLayout.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent(mContext, MapActivity.class);
				intent.putExtra("gps", mdatas.get(position).getGPS());
				mContext.startActivity(intent);
			}
		});

		double Longitude = 0;
		double Latitude = 0;
		String gps = mdatas.get(position).getGPS();
		try {

			if (!TextUtils.isEmpty(gps)) {
				String[] gpsArray = gps.split(",");
				if (gpsArray != null && gpsArray.length == 2) {
					Longitude = Double.parseDouble(gpsArray[0]);
					Latitude = Double.parseDouble(gpsArray[1]);
				}
			}
		} catch (NumberFormatException e) {

		}
		final LatLng addressLatLng = new LatLng(Latitude, Longitude);
		double distance = 0;
		if (localLatLng != null && !TextUtils.isEmpty(gps)) {
			distance = DistanceUtil.getDistance(localLatLng, addressLatLng);
			String unit = "米";
			if (distance >= 1000) {
				distance = distance / 1000;
				unit = "公里";
			}
			DecimalFormat df = new DecimalFormat("0");
			mViewHolder.distanceTextView.setText("距离" + df.format(distance) + unit);
		} else {
			mViewHolder.distanceTextView.setText("距离" + "0m");
		}
		if (isShowStartTask) {
			mViewHolder.taskButton.setVisibility(View.VISIBLE);
			mViewHolder.taskButton.setText("开始任务");
			mViewHolder.taskButton.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View arg0) {
					LocationManager locationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
					if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
						Toast.makeText(mContext, "请打开GPS", Toast.LENGTH_SHORT).show();
						return;
					}
					if (localLatLng != null) {
						final double distance = DistanceUtil.getDistance(localLatLng, addressLatLng);
						// final double distance = 0;
						try {
							final double execRange = Double.parseDouble(mdatas.get(position).getExecRange());
							if (distance <= execRange) {
								Intent intent = new Intent(mContext, SubTaskActivity.class);
								intent.putExtra("taskid", mdatas.get(position).getTask_ID());
								mContext.startActivity(intent);
							} else {
								TiPsDialog tiPsDialog = new TiPsDialog(mContext, R.style.dialog);
								tiPsDialog.show();
							}

						} catch (NumberFormatException e) {
						}
					} else {
						Toast.makeText(mContext, "位置还未定位好！", Toast.LENGTH_SHORT).show();
					}
				}
			});
		} else {
			mViewHolder.taskButton.setVisibility(View.GONE);
		}

		return convertView;
	}

	public final class ViewHolder {

		public TextView addressTextView;
		public TextView distanceTextView;
		public TextView addressDetailsTextView;
		public LinearLayout watchAddressLinearLayout;
		public Button taskButton;
	}

}
